<template>
  <div class="cart">
    <van-nav-bar title="购物车" fixed />
      <div v-if="token && goodsTotal > 0">
        <!-- 购物车开头 -->
        <div class="cart-title">
          <span class="all">共<i>{{ goodsTotal }}</i>件商品</span>
          <span @click="isEd = !isEd" class="edit">
            <van-icon name="edit" />
            编辑
          </span>
        </div>

        <!-- 购物车列表 -->
        <div class="cart-list">
          <div class="cart-item" v-for="item in cartList" :key="item.goods_id">
            <van-checkbox :value="item.isChecked" @click="addGoodChecked(item.goods_id)"></van-checkbox>
            <div class="show">
              <img :src="item.goods.goods_image" alt="">
            </div>
            <div class="info">
              <span class="tit text-ellipsis-2">{{ item.goods.goods_name }}</span>
              <span class="bottom">
                <div class="price">¥ <span>{{ item.goods.goods_price_max }}</span></div>
                <CountBox
                  :value="item.goods_num"
                  @input="(value) => updateCart({
                    goodsId: item.goods_id,
                    goodsNum: value,
                    goodsSkuId: item.goods_sku_id
                  })"
                >
                </CountBox>
              </span>
            </div>
          </div>
        </div>

        <div class="footer-fixed">
          <div class="all-check">
            <van-checkbox @click="addAllChecked()" :value="setAllChecked"  icon-size="18"></van-checkbox>
            全选
          </div>

          <div class="all-total">
            <div class="price">
              <span>合计：</span>
              <span>¥ <i class="totalPrice">{{ goodsCheckedPriceTotal }}</i></span>
            </div>
            <div @click="goPay" v-if="!isEd" :class="{disabled: goodsCheckedList.length === 0}" class="goPay">
              结算({{ goodsCheckedList.length }})
            </div>
            <div v-else class="delete" @click="deleteCart">删除</div>
          </div>
        </div>
      </div>
      <!-- 购物车为空 -->
      <div class="empty-cart" v-else>
        <img src="@/assets/empty.png" alt="">
        <div class="tips">
          您的购物车是空的, 快去逛逛吧
        </div>
        <div class="btn" @click="$router.push('/')">去逛逛</div>
      </div>
  </div>
</template>

<script>
import CountBox from '@/components/CountBox' // 引入计数器组件
import { mapState, mapGetters } from 'vuex' // 引入mapState mapGetters

export default {
  name: 'CartPage',
  components: {
    CountBox
  },
  data () {
    return {
      isEd: false // 是否编辑
    }
  },
  watch: {
    // 监听isEd
    isEd (newVal) {
      if (newVal) {
        this.$store.commit('cart/addAllChecked', false)
      } else {
        this.$store.commit('cart/addAllChecked', true)
      }
    }
  },
  created () {
    if (this.token) {
      this.$store.dispatch('cart/getCartList') // 获取购物车列表
    }
  },
  computed: {
    ...mapGetters(['token']), // 获取用户信息
    ...mapState('cart', ['cartList']), // 获取购物车列表
    ...mapGetters('cart', ['goodsTotal', 'goodsCheckedList', 'goodsCheckedPriceTotal', 'setAllChecked']) // 获取商品总数、选中商品列表、选中商品总价
  },
  methods: {
    // 修改商品选中状态
    addGoodChecked (id) {
      this.$store.commit('cart/addGoodChecked', id)
    },
    // 全选
    addAllChecked () {
      this.$store.commit('cart/addAllChecked', !this.setAllChecked)
    },
    // 修改商品数量
    updateCart (obj) {
      this.$store.dispatch('cart/updateCartAsync', obj)
    },
    // 删除购物车
    deleteCart () {
      if (!this.goodsCheckedList.length) return
      this.$store.dispatch('cart/deleteCartAsync')
    },
    // 去支付
    goPay () {
      if (!this.goodsCheckedList.length) return
      this.$router.push({
        path: '/orders',
        query: {
          mode: 'cart',
          cartIds: this.goodsCheckedList.map(item => item.id).join(',')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
// 主题 padding
.cart {
  padding-top: 46px;
  padding-bottom: 100px;
  background-color: #f5f5f5;
  min-height: 100vh;
  .cart-title {
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    font-size: 14px;
    .all {
      i {
        font-style: normal;
        margin: 0 2px;
        color: #fa2209;
        font-size: 16px;
      }
    }
    .edit {
      .van-icon {
        font-size: 18px;
      }
    }
  }

  .cart-item {
    margin: 0 10px 10px 10px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    background-color: #ffffff;
    border-radius: 5px;

    .show img {
      width: 100px;
      height: 100px;
    }
    .info {
      width: 210px;
      padding: 10px 5px;
      font-size: 14px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .bottom {
        display: flex;
        justify-content: space-between;
        .price {
          display: flex;
          align-items: flex-end;
          color: #fa2209;
          font-size: 12px;
          span {
            font-size: 16px;
          }
        }
        .count-box {
          display: flex;
          width: 110px;
          .add,
          .minus {
            width: 30px;
            height: 30px;
            outline: none;
            border: none;
          }
          .inp {
            width: 40px;
            height: 30px;
            outline: none;
            border: none;
            background-color: #efefef;
            text-align: center;
            margin: 0 5px;
          }
        }
      }
    }
  }
}

.footer-fixed {
  position: fixed;
  left: 0;
  bottom: 50px;
  height: 50px;
  width: 100%;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;

  .all-check {
    display: flex;
    align-items: center;
    .van-checkbox {
      margin-right: 5px;
    }
  }

  .all-total {
    display: flex;
    line-height: 36px;
    .price {
      font-size: 14px;
      margin-right: 10px;
      .totalPrice {
        color: #fa2209;
        font-size: 18px;
        font-style: normal;
      }
    }

    .goPay, .delete {
      min-width: 100px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      background-color: #fa2f21;
      color: #fff;
      border-radius: 18px;
      &.disabled {
        background-color: #ff9779;
      }
    }
  }

}

.empty-cart {
  padding: 80px 30px;
  img {
    width: 140px;
    height: 92px;
    display: block;
    margin: 0 auto;
  }
  .tips {
    text-align: center;
    color: #666;
    margin: 30px;
  }
  .btn {
    width: 110px;
    height: 32px;
    line-height: 32px;
    text-align: center;
    background-color: #fa2c20;
    border-radius: 16px;
    color: #fff;
    display: block;
    margin: 0 auto;
  }
}
</style>
